package com.gitee.zycra.union.api.common.bo;

import com.gitee.zycra.union.api.common.enums.CommonEnum;
import com.gitee.zycra.union.api.common.enums.CommonResultEnum;
import lombok.Getter;
import lombok.ToString;

/**
 * @param <T> 返回值类型
 * @author zycra
 */
@Getter
@ToString
public final class CommonResult<T> {

    private final Integer code;

    private final String message;

    private final T data;

    private CommonResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> CommonResult<T> success() {
        return success(null);
    }

    public static <T> CommonResult<T> success(T data) {
        return build(CommonResultEnum.SUCCESS, data);
    }

    public static <T> CommonResult<T> success(String message, T data) {
        return new CommonResult<>(CommonResultEnum.SUCCESS.getCode(), message, data);
    }

    public static <T> CommonResult<T> failed() {
        return failed(null);
    }

    public static <T> CommonResult<T> failed(String message) {
        return failed(message, null);
    }

    public static <T> CommonResult<T> failed(String message, T data) {
        return new CommonResult<>(CommonResultEnum.FAILED.getCode(), message, data);
    }

    public static <T> CommonResult<T> exception() {
        return exception(null);
    }

    public static <T> CommonResult<T> exception(T data) {
        return build(CommonResultEnum.EXCEPTION, data);
    }

    public static <T, E extends Enum<E>> CommonResult<T> build(CommonEnum<E> commonEnum, T data) {
        return new CommonResult<>(commonEnum.getCode(), commonEnum.getDesc(), data);
    }

    public boolean isSuccess() {
        return CommonResultEnum.SUCCESS.getCode().equals(this.getCode());
    }

    public boolean isException() {
        return CommonResultEnum.EXCEPTION.getCode().equals(this.getCode());
    }
}
